<?php

namespace App\Http\Controllers;

use App\Service\BookService;
use App\Service\UserService;
use Illuminate\Http\Request;


class UserController extends Controller
{
    private $userService;
    private $bookService;

    /**
     * UserController constructor.
     */
    public function __construct()
    {
        $this->userService = new UserService();
        $this->bookService = new BookService();
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $UserInfo = session('user');
        $borrowInfo = $this->userService->getBorrow($UserInfo['userid']);
        $bookInfo = array();
        foreach ($this->bookService->getAllBook() as $value) {
            $bookInfo[$value['bookid']] = $value;
        }

        $data = array(
            'borrowInfo' => $borrowInfo,
            'UserInfo' => $UserInfo,
            'bookInfo' => $bookInfo,
        );
        return view('user',$data);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        switch ($id) {
            //还书
            case 'rent':
                return $this->bookService->rentBook(
                    $request->input('bookid'),
                    $request->input('borrowid')
                );
            case 'last':
                //print_r($request->input('borrowid'));die;
                return $this->bookService->lastBook($request->input('borrowid'));
            default:
                break;
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
